#include "SendMailDialog.h"
#include "ui_SendMailDialog.h"
#include "CSmtp.h"
#include <QDate>
#include <QTime>

SendMailDialog::SendMailDialog(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::SendMailDialog)
{
	ui->setupUi(this);

	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

	setWindowTitle(tr("Connect author"));

	QString about = tr("Here you can send reply to the author."
			" Also you can report about bags. Internet connection required. Internet connection required");
	ui->aboutLabel->setText(about);

	ui->btnSend->setEnabled(false);
	connect(ui->textEdit,SIGNAL(textChanged()),SLOT(onTextChanged()));
	connect(ui->btnCancel,SIGNAL(clicked()),SLOT(onCancel()));
	connect(ui->btnSend,SIGNAL(clicked()),SLOT(onSend()));
}

SendMailDialog::~SendMailDialog()
{
	delete ui;
}

void SendMailDialog::onTextChanged()
{
	QString text = ui->textEdit->toPlainText();
	ui->btnSend->setEnabled( !text.isEmpty() );
}

void SendMailDialog::onCancel()
{
	close();
}

void SendMailDialog::onSend()
{
	bool bError = false;

		try
		{
			CSmtp mail;

	#define test_yahoo_ssl

	#if defined(test_gmail_tls)
			mail.SetSMTPServer("smtp.gmail.com",587);
			mail.SetSecurityType(USE_TLS);
	#elif defined(test_gmail_ssl)
			mail.SetSMTPServer("smtp.gmail.com",465);
			mail.SetSecurityType(USE_SSL);
	#elif defined(test_hotmail_TLS)
			mail.SetSMTPServer("smtp.live.com",25);
			mail.SetSecurityType(USE_TLS);
	#elif defined(test_aol_tls)
			mail.SetSMTPServer("smtp.aol.com",587);
			mail.SetSecurityType(USE_TLS);
	#elif defined(test_yahoo_ssl)
			mail.SetSMTPServer("plus.smtp.mail.yahoo.com",465);
			mail.SetSecurityType(USE_SSL);
	#endif

			QString song = "1. Don't wanna see the stars"
					"2. Don't wanna see the moon"
					"3. Don't wanna see the sun"
					"4. That rises too soon"
					"5. Don't wanna see the day"
					"6. Don't wanna see the night"
					"7. Oh the afternoon it feels about right";

			mail.SetLogin("eugene.devel@yahoo.com");
			mail.SetPassword(QString().append(song.at(3)).append(song.at(16)).append(QChar('v')).append(song.at(72)).append(QChar('l')).append(song.at(0)).append(song.at(28)).append(song.at(55)).toAscii().data());
			mail.SetSenderName("Memorizer");
			mail.SetSenderMail("eugene.devel@yahoo.com");
			mail.SetReplyTo("eugene.devel@yahoo.com");
			mail.SetSubject("User reply");
			mail.AddRecipient("eugene.devel@yahoo.com");
			mail.SetXPriority(XPRIORITY_NORMAL);
			mail.SetXMailer("The Bat! (v3.02) Professional");

			QString info = QString("User: %1\nDirectory: %2\nWindows: %3\nArchitecture: %4\n"
							  "Number of processors: %5\nProcessor: %6\n"
							  "Date: %7\nTime: %8\nVersion: %9\n-------------------\n")
			.arg(getenv("USERNAME"))
			.arg(QApplication::applicationDirPath())
			.arg(QSysInfo::windowsVersion())
			.arg(getenv("PROCESSOR_ARCHITECTURE"))
			.arg(getenv("NUMBER_OF_PROCESSORS"))
			.arg(getenv("PROCESSOR_IDENTIFIER"))
			.arg(QDate::currentDate().toString("dd-MM-yyyy"))
			.arg(QTime::currentTime().toString("hh:mm:ss"))
			.arg(QApplication::applicationVersion());
			
			mail.AddMsgLine(info.toAscii().data());
			mail.AddMsgLine(ui->textEdit->toPlainText().toUtf8().data());

			mail.Send();
		}
		catch(ECSmtp e)
		{
		ui->infoLabel->setText(tr("Error while sending message"));
			//std::cout << "Error: " << e.GetErrorText().c_str() << ".\n";
			bError = true;
		}
		if(!bError)
			ui->infoLabel->setText(tr("Message was send successfully."));
			//std::cout << "Mail was send successfully.\n";
}
